
//
//  DataPickerVie.m
//  MicroAPP
//
//  Created by 陈区 on 13-8-22.
//  Copyright (c) 2013年 陈区. All rights reserved.
//

#import "DatePickerView.h"

@implementation DatePickerView
@synthesize datePicker = m_datePicker;
@synthesize delegate;
@synthesize space = m_space;
@synthesize formatTime = m_formatTime;
@synthesize myValue =_myValue;


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.bounds), 40)];
        toolBar.tintColor = kColor_1;
        toolBar.barTintColor = [UIColor seg_setColorWithAny:kColor_bg dark:kColor_W2];
		[self addSubview:toolBar];
		
		UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"  取消", nil)
                                                                      style:UIBarButtonItemStyleDone
                                                                     target:self
                                                                     action:@selector(btnCancelClick:)];
        
		UIBarButtonItem *sureBtn = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"确认", nil)
																	style:UIBarButtonItemStyleDone
																   target:self
																   action:@selector(btnOKClick:)];
		[sureBtn setTag:10];
        
        self.space = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, CGRectGetWidth(self.bounds)-120, 20)];
        self.space.textColor = kColor_W;
        self.space.textAlignment = NSTextAlignmentCenter;
        self.space.font = [UIFont systemFontOfSize:16.0];
        self.space.backgroundColor = [UIColor clearColor];
        UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithCustomView:self.space];
        space.enabled = YES;

		[toolBar setItems:[NSArray arrayWithObjects:cancelBtn,space,sureBtn,nil]];
        
		self.datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 40, LYScreenWidth(), 216)];
        if (@available(iOS 13.4, *)) {
            self.datePicker.preferredDatePickerStyle = UIDatePickerStyleWheels;
        }
		self.datePicker.datePickerMode = UIDatePickerModeDate;
        self.backgroundColor=kColor_W;
        NSLocale *locale = [[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"];
        self.datePicker.locale = locale;
        [self.datePicker addTarget: self action: @selector(changes) forControlEvents: UIControlEventValueChanged];
		[self addSubview:self.datePicker];
        [self.datePicker mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.right.bottom.mas_equalTo(0);
                    make.top.mas_equalTo(40);
        }];
    }
    return self;
}

#pragma mark - Button Click Event

- (void)btnCancelClick:(id)sender
{
    if ([delegate respondsToSelector:@selector(didDatePickerViewCancelClicked:)])
    {
        [delegate didDatePickerViewCancelClicked:self];
    }
}

- (void)btnOKClick:(id)sender
{
    if ([delegate respondsToSelector:@selector(didDatePickerViewDoneClicked:)])
    {
        [delegate didDatePickerViewDoneClicked:self];
    }
}

-(void)changes
{
    if (self.formatTime) {
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        
        
        [dateFormatter setDateFormat:self.formatTime];
        
        if (self.datePicker.date)
        {
            NSString *value = [dateFormatter stringFromDate:self.datePicker.date];
            
            self.space.text = value;
        }
    }

    
}

-(NSDate *)selectDate
{
    return self.datePicker.date;
}

@end


